<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Article;
use App\Models\FamilyType;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new \App\Hospital\Repositories\Article(['familyType','info']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id');

            $grid->column('info.title','文章标题');
            $grid->familyType('针推人群')->pluck('name')->label();
            $grid->column('type')->using(\App\Models\Article::$typeEnum)->label([

                1 => 'primary',
                2 => 'danger',
                3 => 'success',
            ]);
            $grid->column('status')->using([
                '0' => '待审核',
                '1' => '审核通过',
                '2' => '审核失败',
            ])->label([

                0 => 'primary',
                1 => 'success',
                2 => 'danger',
            ]);
            $grid->column('is_push','是否推送')
                ->if(function ($column) {

                    return $this->type == 1;
                })
                ->switch('', true)->else()
                ->using([
                    0 => '否',
                    1 => '是',
                ]);;
            $grid->disableBatchDelete();//禁用批量操作
            $grid->disableRowSelector();//禁用行选择器

//            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                if($this->hospital_id != Admin::user()->hospital_id){
//                    $actions->disableDelete();
//                    $actions->disableEdit();
//                    $actions->disableQuickEdit();
//                }
//            });
            $grid->filter(function($filter){
                // 禁止自动展开过滤器
                $filter->expand(false);
                $filter->panel();

                $filter->like('info.title', '文章标题');
                $filter->like('info.content', '文章内容');
                //$filter->like('type', '文章内容');
                //$filter->in('id')->multipleSelect(); // 设置编辑数据显示


            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('status');
            $show->field('type');
            $show->field('subject_pid');
            $show->field('subject');
            $show->field('hospital_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $a = FamilyType::query()->pluck('name','id');
        return Form::make(Article::with(['info','familyType']), function (Form $form) {
            $form->text('info.title','文章标题');
            $form->text('info.ftitle','文章副标题');
            $form->image('info.img','文章封面')
                ->uniqueName()
                ->autoUpload()
                ->accept('jpg,png,gif,jpeg', 'image/*')
                ->maxSize(500)->disk('oss');
            $form->editor('info.content','文章内容')->disk('oss');;
            $form->text('info.source','来源')->value('儿宝宝');
//
//
//
            $form->multipleSelect('familyType','文章对应人群')
                ->options(FamilyType::query()->pluck('name','id'))
                ->customFormat(function ($v) {
                    if (! $v) {
                        return [];
                    }

                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                });

            $form->select('type')->options(\App\Models\Article::$typeEnum)->default(2);
            $form->text('subject_pid')->value(0);
            $form->text('subject')->value(0);
            $form->text('hospital_id')->value(0);
            $form->select('status')->options([
                '0' => '待审核',
                '1' => '审核通过',
                '2' => '审核失败',
            ])->default(0);
            $form->switch('is_push','是否推送')
                ->saving(function ($v) {
                    return $v ? 1 : 0;
                });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
